package net.samsb.request.execute;

import android.os.Message;
import android.util.Log;

import net.samsb.BaseApplication;
import net.samsb.log.LogUtils;
import net.samsb.tool.HttpClient;
import net.samsb.tool.Tools;

import java.io.IOException;

import okhttp3.Response;

import static net.samsb.activity.Constants.*;

/**
 * Created by Destiny_hao on 2018/3/26.
 */

public class PictureExecute implements HttpClient {

    private Message message;

    @Override
    public void onSucceed(int what, Response response, int arg1) {

        if (!response.isSuccessful()) {
            Tools.Toast("请求错误，请检查网络是否连接");
            return;
        }

        byte[] Picture_bt = null;

        switch (what) {
            case HOME_GET_WEATHER_IMG_UI: {

                try {

                    Picture_bt = response.body().bytes();

                } catch (IOException e) {
                    e.printStackTrace();
                }

                message = BaseApplication.mHandler
                        .getHandlerWithName("MainActivity")
                        .obtainMessage();

                if (arg1 == UI_UPDATE_WEATHER_NOW) {
                    arg1 = UI_UPDATE_WEATHER_NOW;
                }

                if (arg1 == UI_UPDATE_WEATHER_ONE) {
                    arg1 = UI_UPDATE_WEATHER_ONE;
                }

                if (arg1 == UI_UPDATE_WEATHER_TWO) {
                    arg1 = UI_UPDATE_WEATHER_TWO;
                }

                if (arg1 == UI_UPDATE_WEATHER_THREE) {
                    arg1 = UI_UPDATE_WEATHER_THREE;
                }

                if (arg1 == UI_UPDATE_AVATAR) {
                    arg1 = UI_UPDATE_AVATAR;
                }

                message.what = HOME_GET_WEATHER_IMG_UI;
                message.arg1 = arg1;
                message.obj = Tools.bytesToBitmap(Picture_bt);

                BaseApplication.mHandler
                        .getHandlerWithName("MainActivity")
                        .sendMessage(message);

                break;
            }

            case HOME_GET_AVATAR_IMG_UI: {

                try {

                    Picture_bt = response.body().bytes();

                } catch (IOException e) {
                    e.printStackTrace();
                }

                message = BaseApplication.mHandler
                        .getHandlerWithName("MainActivity")
                        .obtainMessage();

                message.what = HOME_GET_AVATAR_IMG_UI;
                message.obj = Tools.bytesToBitmap(Picture_bt);

                BaseApplication.mHandler
                        .getHandlerWithName("MainActivity")
                        .sendMessage(message);
                break;
            }
        }
    }

    @Override
    public void onFailed(int what, String msg) {

        LogUtils.e("PictureRequest", msg);

        message = BaseApplication.mHandler
                .getHandlerWithName("MainActivity")
                .obtainMessage();

        message.what = ERROR;
        //message.obj = msg;

        BaseApplication.mHandler
                .getHandlerWithName("MainActivity")
                .sendMessage(message);

        // MyDialog.showDialog(MyActivityManager.getInstance().getCurrentActivity(), "请求失败，请检查网络是否连接");

    }
}
